package xcmg.device.service.ratingManager;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.ratingManager.RatingInfoDO;
import xcmg.device.dao.mapper.ratingManager.RatingInfoDOMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.ratingManager.RatingInfoQueryVO;
import xcmg.device.service.vo.ratingManager.RatingInfoVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by wrg on 2019/7/10.
 */
@Service
public class RatingInfoService {
    @Autowired
    private RatingInfoDOMapper ratingInfoDOMapper;
    @Autowired
    private FastGenClient genClient;

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode add(RatingInfoVO ratingInfoVO) throws Exception {
        RatingInfoDO ratingInfoDO = new RatingInfoDO();
        BeanUtils.copyProperties(ratingInfoVO, ratingInfoDO);
        String id = genClient.newGuidText().getValue();
        if (StringUtils.isBlank(id)) {
            return ErrorCode.IllegalArument;
        }
        ratingInfoDO.setId(id);
        ratingInfoDO.setCreateTime(new Date());
        return ratingInfoDOMapper.insertSelective(ratingInfoDO) > 0 ? ErrorCode.Success : ErrorCode.Failure;
    }

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode remove(List<String> ids) throws Exception {
        for (String id : ids) {
            int deleteCount = ratingInfoDOMapper.deleteByPrimaryKey(id);
            if (deleteCount <= 0) {
                throw new Exception("删除失败");
            }
            // todo 删除其它关联信息
        }

        return ErrorCode.Success;
    }


    @Transactional(rollbackFor = Exception.class)
    public ErrorCode update(RatingInfoVO ratingInfoVO, String userId, String userName) throws Exception {
        RatingInfoDO record = new RatingInfoDO();
        record.setId(ratingInfoVO.getId());
        record.setModifierId(userId);
        record.setModifierName(userName);
        record.setModifyTime(new Date());
        record.setTitle(ratingInfoVO.getTitle());
        record.setRemark(ratingInfoVO.getRemark());
        return ratingInfoDOMapper.updateByPrimaryKeySelective(record) > 0 ? ErrorCode.Success : ErrorCode.Failure;
    }

    public PageCommonVO list(SearchCommonVO<RatingInfoQueryVO> condition) {
        PageHelperPlus.orderBy("create_time,id desc");
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        List<RatingInfoDO> billDOList = ratingInfoDOMapper.list(condition.getFilters());
        List<RatingInfoVO> voList = new ArrayList<>();
        for (RatingInfoDO ratingInfoDO : billDOList) {
            RatingInfoVO ratingInfoVO = new RatingInfoVO();
            BeanUtils.copyProperties(ratingInfoDO, ratingInfoVO);
            voList.add(ratingInfoVO);
        }
        PageCommonVO<RatingInfoVO> result = new PageCommonVO<>();
        result.setPageInfo(new PageInfo<>(billDOList));
        result.setPageInfoList(voList);
        return result;
    }

    public RatingInfoVO item(String id) {
        RatingInfoDO ratingInfoDO = ratingInfoDOMapper.selectByPrimaryKey(id);
        RatingInfoVO ratingInfoVO = new RatingInfoVO();
        if (null != ratingInfoDO) {
            BeanUtils.copyProperties(ratingInfoDO, ratingInfoVO);
        }
        return ratingInfoVO;
    }
}
